
from urllib.parse import quote_plus

from app.utils.utils_loader import parse_args


"""
    FastAPI Config
"""
fast_api_config = {
    "title": "FastAPI",
    "version": "0.1.0",
    "openapi_url": "/openapi.json",
    "docs_url": "/docs",
    "redoc_url": "/redoc",
}


"""
    雪花算法 app/utils/utils_pysnowflake.py
"""

DATACENTER_ID = parse_args("DATACENTER_ID", "1", int)
WORKER_ID = parse_args("WORKER_ID", "1", int)
SEQUENCE = parse_args("SEQUENCE", "1", int)

"""
    数据库配置
"""
DB_DIALECT_DRIVER = parse_args("DB_DIALECT_DRIVER", "postgresql")
DB_USER = parse_args("DB_USER", "azpsqladmin")
DB_PWD = parse_args("DB_PWD", "$k|:dObEuTOswT3nc=!U")
DB_HOST = parse_args("DB_HOST", "eagledmpgsql-dev.apac.le.grp")
DB_PORT = parse_args("DB_PORT", "5432")
DB_NAME = parse_args("DB_NAME", "test_fastapi")
SQLALCHEMY_DATABASE_URI = f"{DB_DIALECT_DRIVER}://{DB_USER}:{quote_plus(DB_PWD)}@{DB_HOST}:{DB_PORT}/{DB_NAME}"

USE_SQLALCHEMY_BINDS = parse_args("USE_SQLALCHEMY_BINDS", False, bool)

if USE_SQLALCHEMY_BINDS:
    SQLALCHEMY_BINDS = {
        'default': SQLALCHEMY_DATABASE_URI,
    }

USE_DB_POOL = parse_args("DB_POOL_USE", True, bool)

SQLALCHEMY_ENGINE_OPTIONS = dict(
    # 链接池数量
    pool_size=parse_args("DB_POOL_SIZE", 5, int),

    # 当连接池中链接已经用完了, 最多还允许建立多少额外的链接
    max_overflow=parse_args("DB_POOL_MAX_OVERFLOW", 200, int),

    # 指定池的连接超时, 秒为单位
    pool_timeout=parse_args("DB_POOL_TIMEOUT", 30, int),

    # 连接池回收时间, 秒为单位
    # 这个值要小于mysql的max_timeout, 否则会lost connection
    pool_recycle=parse_args("DB_POOL_RECYCLE", 60, int),
)


"""
    分页配置
"""
QUERY_ALL_PAGE_SIZE = parse_args("QUERY_ALL_PAGE_SIZE", 500, int)
QUERY_DEFAULT_PAGE_SIZE = parse_args("QUERY_DEFAULT_PAGE_SIZE", 10, int)
